package com.macbulldev.poc.testgin.domain;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.Transient;
import javax.persistence.Version;

import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.transaction.annotation.Transactional;


@Configurable
@javax.persistence.Entity
@org.springframework.roo.addon.javabean.RooJavaBean
@org.springframework.roo.addon.tostring.RooToString
@org.springframework.roo.addon.entity.RooEntity
public class Tier {
	
	@NotEmpty(message="Cannot be empty")
    private BigDecimal subtractAmt;
	
	@NotEmpty(message="Cannot be empty")
    private BigDecimal tierAddAmt;
	
	@NotEmpty(message="Cannot be empty")
    private BigDecimal tierMultiplyAmt;

	@NotEmpty(message="Cannot be empty")
    private BigDecimal tierUpperAmt;
	
	@NotEmpty(message="Cannot be empty")
    private BigDecimal tierLowerAmt;
	
	@NotEmpty(message="Cannot be empty")
    private BigDecimal tierDivideAmt;
    
    
    @javax.persistence.ManyToOne(targetEntity = com.macbulldev.poc.testgin.domain.Rate.class)
    @javax.persistence.JoinColumn(name="rateId")
    private com.macbulldev.poc.testgin.domain.Rate rate;
    
    private String createUsername;
    private String updateUsername;

    @javax.persistence.Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @org.springframework.format.annotation.DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS")
    private java.util.Date createTs;
    
    @javax.persistence.Temporal(javax.persistence.TemporalType.TIMESTAMP)
    @org.springframework.format.annotation.DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS")
    private java.util.Date updateTs;
    
    @Transient
    private StateOrProvince stateOrProvince;
    
    private boolean isFlatCharge;
    
	public StateOrProvince getStateOrProvince() {
		return stateOrProvince;
	}

	
	public void setStateOrProvince(StateOrProvince stateOrProvince) {
		this.stateOrProvince = stateOrProvince;
	}
	
	public boolean getIsFlatCharge() {
		return isFlatCharge;
	}

	
	public void setIsFlatCharge(boolean isFlatCharge) {
		this.isFlatCharge = isFlatCharge;
	}
	
	public static List<Tier> findTiersByRate(Long rateId) {    
        
    	String hql = "select t from Tier t where t.rate.id = ? order by t.tierLowerAmt asc";
    	
    	EntityManager entityManager = new Tier().entityManager;
    	
    	Query q = entityManager().createQuery(hql);
    	q.setParameter(1,rateId);
        return q.getResultList();
    }
    
    

	@PersistenceContext    
    transient EntityManager entityManager;

	@Id    
    @GeneratedValue(strategy = GenerationType.AUTO)    
    @Column(name = "id")    
    private Long id;

	@Version    
    @Column(name = "version")    
    private Integer version;

	public Long getId() {    
        return this.id;        
    }

	public void setId(Long id) {    
        this.id = id;        
    }

	public Integer getVersion() {    
        return this.version;        
    }

	public void setVersion(Integer version) {    
        this.version = version;        
    }

	@Transactional    
    public void persist() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        this.entityManager.persist(this);        
    }

	@Transactional    
    public void remove() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        if (this.entityManager.contains(this)) {        
            this.entityManager.remove(this);            
        } else {        
            Tier attached = this.entityManager.find(Tier.class, this.id);            
            this.entityManager.remove(attached);            
        }        
    }

	@Transactional    
    public void flush() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        this.entityManager.flush();        
    }

	@Transactional    
    public void merge() {    
        if (this.entityManager == null) this.entityManager = entityManager();        
        Tier merged = this.entityManager.merge(this);        
        this.entityManager.flush();        
        this.id = merged.getId();        
    }

	public static final EntityManager entityManager() {    
        EntityManager em = new Tier().entityManager;        
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");        
        return em;        
    }

	public static long countTiers() {    
        return (Long) entityManager().createQuery("select count(o) from Tier o").getSingleResult();        
    }

	public static List<Tier> findAllTiers() {    
        return entityManager().createQuery("select o from Tier o").getResultList();        
    }

	public static Tier findTier(Long id) {    
        if (id == null) throw new IllegalArgumentException("An identifier is required to retrieve an instance of Tier");        
        return entityManager().find(Tier.class, id);        
    }

	public static List<Tier> findTierEntries(int firstResult, int maxResults) {    
        return entityManager().createQuery("select o from Tier o").setFirstResult(firstResult).setMaxResults(maxResults).getResultList();        
    }

	public BigDecimal getSubtractAmt() {    
        return this.subtractAmt;        
    }

	public void setSubtractAmt(BigDecimal subtractAmt) {    
        this.subtractAmt = subtractAmt;        
    }

	public BigDecimal getTierAddAmt() {    
        return this.tierAddAmt;        
    }

	public void setTierAddAmt(BigDecimal tierAddAmt) {    
        this.tierAddAmt = tierAddAmt;        
    }

	public BigDecimal getTierMultiplyAmt() {    
        return this.tierMultiplyAmt;        
    }

	public void setTierMultiplyAmt(BigDecimal tierMultiplyAmt) {    
        this.tierMultiplyAmt = tierMultiplyAmt;        
    }

	public BigDecimal getTierUpperAmt() {    
        return this.tierUpperAmt;        
    }

	public void setTierUpperAmt(BigDecimal tierUpperAmt) {    
        this.tierUpperAmt = tierUpperAmt;        
    }

	public BigDecimal getTierLowerAmt() {    
        return this.tierLowerAmt;        
    }

	public void setTierLowerAmt(BigDecimal tierLowerAmt) {    
        this.tierLowerAmt = tierLowerAmt;        
    }

	public BigDecimal getTierDivideAmt() {    
        return this.tierDivideAmt;        
    }

	public void setTierDivideAmt(BigDecimal tierDivideAmt) {    
        this.tierDivideAmt = tierDivideAmt;        
    }

	public Rate getRate() {    
        return this.rate;        
    }

	public void setRate(Rate rate) {    
        this.rate = rate;        
    }

	public String getCreateUsername() {    
        return this.createUsername;        
    }

	public void setCreateUsername(String createUsername) {    
        this.createUsername = createUsername;        
    }

	public String getUpdateUsername() {    
        return this.updateUsername;        
    }

	public void setUpdateUsername(String updateUsername) {    
        this.updateUsername = updateUsername;        
    }

	public Date getCreateTs() {    
        return this.createTs;        
    }

	public void setCreateTs(Date createTs) {    
        this.createTs = createTs;        
    }

	public Date getUpdateTs() {    
        return this.updateTs;        
    }

	public void setUpdateTs(Date updateTs) {    
        this.updateTs = updateTs;        
    }

	public boolean isIsFlatCharge() {    
        return this.isFlatCharge;        
    }

	public String toString() {    
        StringBuilder sb = new StringBuilder();        
        sb.append("StateOrProvince: ").append(getStateOrProvince()).append(", ");        
        sb.append("IsFlatCharge: ").append(isIsFlatCharge()).append(", ");        
        sb.append("Id: ").append(getId()).append(", ");        
        sb.append("Version: ").append(getVersion()).append(", ");        
        sb.append("SubtractAmt: ").append(getSubtractAmt()).append(", ");        
        sb.append("TierAddAmt: ").append(getTierAddAmt()).append(", ");        
        sb.append("TierMultiplyAmt: ").append(getTierMultiplyAmt()).append(", ");        
        sb.append("TierUpperAmt: ").append(getTierUpperAmt()).append(", ");        
        sb.append("TierLowerAmt: ").append(getTierLowerAmt()).append(", ");        
        sb.append("TierDivideAmt: ").append(getTierDivideAmt()).append(", ");        
        sb.append("Rate: ").append(getRate()).append(", ");        
        sb.append("CreateUsername: ").append(getCreateUsername()).append(", ");        
        sb.append("UpdateUsername: ").append(getUpdateUsername()).append(", ");        
        sb.append("CreateTs: ").append(getCreateTs()).append(", ");        
        sb.append("UpdateTs: ").append(getUpdateTs());        
        sb.append("IsFlatCharge: ").append(isIsFlatCharge());        
        return sb.toString();        
    }
}
